package com.mmall.concurrency;

import com.mmall.concurrency.threadlocal.RequestHolder;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Slf4j
public class HttpFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 首先强转
        HttpServletRequest request = (HttpServletRequest) servletRequest;

        // 打印请求信息
        log.info("执行 filter:{}, {}", Thread.currentThread().getId(), request.getServletPath());

        // request.getSession().getAttribute();  就可以获取信息
        RequestHolder.add(Thread.currentThread().getId());
        // 让拦截器执行完， 让请求继续被处理。
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
